import { Modal } from 'ant-design-vue'
import instance, { AxiosResponse } from 'axios'

/** 接口地址 */
const baseUrl = import.meta.env.VITE_Api


const axios = instance.create({
  timeout: 10000,
  baseURL: baseUrl,
  headers: { 'X-Requested-With': 'XMLHttpRequest'}
})

// 添加请求拦截器
axios.interceptors.request.use((config) => {
  const token = localStorage.getItem('token')
  token && (config.headers['Authorization'] = token)
  return config
})

// 添加响应拦截器
axios.interceptors.response.use(response => {
  handleNetworkResponse(response)
  return response
}, err => {
  handleNetworkError(err.response.status)
  Promise.reject(err.response)
})

const handleNetworkResponse = (response: AxiosResponse<any, any>) => {
  const { data } = response
  if(data.code !== 0){
    if(data.code == 401001) {
      Modal.warning({ okText: '知道了', content: data.message, onOk(){
        localStorage.clear()
        sessionStorage.clear()
        window.location.reload()
      }})
    } else {
      Modal.warning({ okText: '知道了', content: data.message})
    }
    throw response
  }
  return true
}

const handleNetworkError = (errStatus: number) => {
  let errMessage = '未知错误'
  if (errStatus) {
    switch (errStatus) {
      case 400:
        errMessage = '错误的请求'
        break
      case 401:
        errMessage = '未授权，请重新登录'
        break
      case 403:
        errMessage = '拒绝访问'
        break
      case 404:
        errMessage = '请求错误,未找到该资源'
        break
      case 405:
        errMessage = '请求方法未允许'
        break
      case 408:
        errMessage = '请求超时'
        break
      case 500:
        errMessage = '服务器端出错'
        break
      case 501:
        errMessage = '网络未实现'
        break
      case 502:
        errMessage = '网络错误'
        break
      case 503:
        errMessage = '服务不可用'
        break
      case 504:
        errMessage = '网络超时'
        break
      case 505:
        errMessage = 'http版本不支持该请求'
        break
      default:
        errMessage = `其他连接错误 --${errStatus}`
    }
  } else {
    errMessage = `无法连接到服务器！`
  }
  Modal.warning({ okText: '知道了', content: errMessage, icon: null })
}

export default axios
